//! FreeSWITCH SIP客户端基本使用示例

use freeswitch_sip_cli::sip_handler::SipMessageHandler;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 初始化日志
    tracing_subscriber::fmt::init();

    println!("=== FreeSWITCH SIP客户端示例 ===");

    // 创建SIP消息处理器
    let sip_handler = SipMessageHandler::new("127.0.0.1".to_string());

    // 示例1: 解析SIP INVITE请求
    println!("\n1. 解析SIP INVITE请求:");
    let invite_message = r#"INVITE sip:1000@127.0.0.1 SIP/2.0
From: <sip:client@127.0.0.1:5060>
To: <sip:1000@127.0.0.1>
Call-ID: abc123@127.0.0.1
CSeq: 1 INVITE
Contact: <sip:client@127.0.0.1:5060>
Content-Type: application/sdp
Content-Length: 200

v=0
o=client 1234567890 1234567890 IN IP4 127.0.0.1
s=FreeSWITCH SIP CLI Call
c=IN IP4 127.0.0.1
t=0 0
m=audio 10000 RTP/AVP 0 8 101
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16"#;

    match sip_handler.parse_sip_request(invite_message) {
        Ok(request) => {
            println!("✅ 成功解析SIP请求:");
            println!("   方法: {}", request.method);
            println!("   URI: {}", request.uri);
            println!("   Call-ID: {}", request.headers.get("Call-ID").unwrap_or(&"N/A".to_string()));
        }
        Err(e) => {
            println!("❌ 解析SIP请求失败: {}", e);
        }
    }

    // 示例2: 生成SDP
    println!("\n2. 生成SDP:");
    let sdp = sip_handler.create_sdp();
    println!("生成的SDP:\n{}", sdp);

    // 示例3: 创建INVITE请求
    println!("\n3. 创建INVITE请求:");
    let invite = sip_handler.create_invite_request(
        "sip:1000@127.0.0.1",
        "sip:client@127.0.0.1",
        "abc123@127.0.0.1",
        "sip:client@127.0.0.1:5060"
    );
    println!("生成的INVITE请求:\n{}", invite);

    // 示例4: 解析SIP响应
    println!("\n4. 解析SIP响应:");
    let response_message = r#"SIP/2.0 200 OK
From: <sip:client@127.0.0.1:5060>
To: <sip:1000@127.0.0.1>
Call-ID: abc123@127.0.0.1
CSeq: 1 INVITE
Contact: <sip:server@127.0.0.1:5060>
Content-Type: application/sdp
Content-Length: 200

v=0
o=server 1234567890 1234567890 IN IP4 127.0.0.1
s=FreeSWITCH SIP CLI Call
c=IN IP4 127.0.0.1
t=0 0
m=audio 10001 RTP/AVP 0 8 101
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16"#;

    match sip_handler.parse_sip_response(response_message) {
        Ok(response) => {
            println!("✅ 成功解析SIP响应:");
            println!("   状态码: {}", response.status_code);
            println!("   原因短语: {}", response.reason_phrase);
            println!("   Call-ID: {}", response.headers.get("Call-ID").unwrap_or(&"N/A".to_string()));
        }
        Err(e) => {
            println!("❌ 解析SIP响应失败: {}", e);
        }
    }

    println!("\n=== 示例完成 ===");
    Ok(())
} 